import {defineStore} from "pinia";

const useNavInfoStore = defineStore('navInfo',{
    state:() => (
        {
        // 加载状态
        loading: false,

        // 一级导航
        // 默认id
        defaultId: 0,
        // 选中id
        selectId: 0,
        // 选中项
        selectItem:{
            id: 0,
            chinaName: '首页',
            englishName: 'home',
            items:[]
        },

        // 二级导航栏
        // 默认项
        subDefaultItem: {
            id: -1,
            chinaName: '推荐',
            englishName: 'recommend',
        },
        // 选中id
        subSelectId: -1,
        // 选中项
        subSelectItem: {
            id: -1,
            chinaName: '推荐',
            englishName: 'recommend',
        },

        // 导航栏数据
        menus:[],
    }),

    getters:{
        subMenus(){

            let subMenus = [this.subDefaultItem];
            if(this.selectItem.items !== null) {
                subMenus = subMenus.concat(this.selectItem.items);
            }
            return subMenus;
        }
    },
    actions:{
        // 设置第一级nav的选中项和选中id
        setSelect(selectId){
            this.selectId = selectId;
            this.menus.forEach(item=>{
                if(item.id === selectId){
                    this.selectItem = item;
                }
            })
        },
        // 根据英文名称，设置第一级nav的选中项和选中id
        setSelectByName(englishName){
            this.menus.forEach(item=>{
                if(item.englishName === englishName){
                    this.selectItem = item;
                    this.selectId = item.id;
                }
            })
        },

        // 设置第二级nav的选中项和选中id
        setSubSelect(subSelectId){
            this.subSelectId = subSelectId
            this.subMenus.forEach(item => {
                if(item.id === subSelectId){
                    this.subSelectItem = item
                }
            })

        },

        // 根据英文名设置第二级nav的选中项和选中id
        setSubSelectByName(englishName){
            this.subMenus.forEach(item => {
                if(item.englishName === englishName){
                    this.subSelectItem = item;
                    this.subSelectId = item.id;
                }
            })
        },


        setNavValue(menus){
            this.menus = menus;
        }
    },

})

export default useNavInfoStore